<template>
  <div class="register-bg">
    <div class="register-container">
      <div class="register-brand">
        <div class="brand-logo">📊</div>
        <h1>金融工具平台</h1>
        <p>专业的金融分析与投资决策工具</p>
      </div>
      
      <div class="register-card">
        <h2>用户注册</h2>
        <form @submit.prevent="onRegister">
          <div class="form-item">
            <label for="username">用户名</label>
            <div class="input-wrapper">
              <span class="input-icon">👤</span>
              <input 
                id="username"
                v-model="username" 
                type="text" 
                placeholder="请输入用户名" 
                required 
              />
            </div>
          </div>
          
          <div class="form-item">
            <label for="password">密码</label>
            <div class="input-wrapper">
              <span class="input-icon">🔒</span>
              <input 
                id="password"
                v-model="password" 
                type="password" 
                placeholder="请输入密码" 
                required 
              />
            </div>
          </div>
          
          <div class="form-item">
            <label for="confirmPassword">确认密码</label>
            <div class="input-wrapper">
              <span class="input-icon">🔐</span>
              <input 
                id="confirmPassword"
                v-model="confirmPassword" 
                type="password" 
                placeholder="请再次输入密码" 
                required 
              />
            </div>
          </div>
          
          <button class="register-btn" type="submit">
            <span v-if="isLoading" class="loading-spinner"></span>
            <span v-else>注册</span>
          </button>
        </form>
        
        <div class="form-footer">
          <a href="javascript:;" @click="goLogin">已有账号？去登录</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const isLoading = ref(false)
const router = useRouter()

const onRegister = async () => {
  if (!username.value || !password.value || !confirmPassword.value) {
    alert('请填写完整的用户信息')
    return
  }
  
  if (password.value !== confirmPassword.value) {
    alert('两次密码不一致！')
    return
  }
  
  try {
    isLoading.value = true
    const res = await axios.post('/api/register', {
      username: username.value,
      password: password.value
    })
    
    const data = res.data
    if (data.code === 0) {
      alert('注册成功，请登录')
      router.push('/login')
    } else {
      alert(data.msg)
    }
  } catch (e) {
    alert('注册失败，无法连接服务器')
  } finally {
    isLoading.value = false
  }
}

const goLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, var(--info-light), var(--primary-light));
  display: flex;
  align-items: flex-start; /* 改为靠上对齐 */
  justify-content: center;
  padding: 2rem;
  padding-top: 5rem; /* 添加顶部内边距 */
  position: relative;
  overflow: hidden;
}

.register-bg::before {
  content: "";
  position: absolute;
  top: -50%;
  right: -50%;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 60%);
  z-index: 1;
}

.register-container {
  display: flex;
  max-width: 900px;
  width: 100%;
  background: rgba(255, 255, 255, 0.95);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  position: relative;
  z-index: 2;
  border: 1px solid rgba(255, 255, 255, 0.3);
  max-height: 650px; /* 设置最大高度，注册页内容略多，所以高度略大 */
}

.register-brand {
  flex: 1;
  background: linear-gradient(135deg, var(--info-light), var(--primary-light));
  color: rgb(65, 56, 241);
  padding: 3rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.register-brand::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 100%);
}

.brand-logo {
  font-size: 4rem;
  margin-bottom: 1.5rem;
  position: relative;
  height: 100px;
  width: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.register-brand h1 {
  font-size: 2.25rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: relative;
}

.register-brand p {
  font-size: 1rem;
  opacity: 0.9;
}

.register-card {
  flex: 1;
  padding: 3rem;
  background: white;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.register-card h2 {
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--info-dark);
  margin-bottom: 2rem;
  text-align: center;
  position: relative;
}

.form-item {
  margin-bottom: 1.5rem;
}

.form-item label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: var(--neutral-700);
  font-size: 0.95rem;
  letter-spacing: 0.5px;
}

.input-wrapper {
  position: relative;
  border-radius: var(--radius);
  transition: all 0.3s;
}

.input-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1.1rem;
  color: var(--info-color);
  opacity: 0.8;
  transition: all 0.3s;
}

.form-item input:focus + .input-icon {
  color: var(--info-dark);
  opacity: 1;
}

.form-item input {
  width: 100%;
  padding: 0.85rem 0.75rem 0.85rem 2.5rem;
  border: 1px solid var(--neutral-300);
  border-radius: var(--radius);
  font-size: 1rem;
  transition: all 0.2s;
  background-color: var(--neutral-50);
}

.form-item input:focus {
  border-color: var(--info-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
  background-color: rgb(249, 248, 251);
}

.register-btn {
  width: 50%;
  margin: 0 auto;
  padding: 0.6rem;
  background: rgb(148, 144, 230);  /* 与登录页面保持一致的按钮颜色 */
  color: black;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 2.5rem;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(59, 130, 246, 0.2);
}

.register-btn:hover {
  background: #4296ea;
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(30, 144, 255, 0.4);
}

.register-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(30, 144, 255, 0.3);
}

.register-btn::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(rgba(135, 206, 250, 0.3), rgba(30, 144, 255, 0.1));
  opacity: 0;
  transition: opacity 0.2s;
}

.register-btn:hover::after {
  opacity: 1;
}

.form-footer {
  margin-top: 2rem;
  text-align: center;
  position: relative;
}

.form-footer a {
  color: var(--info-dark);
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s;
  padding: 0.4rem 0.8rem;
  border-radius: var(--radius);
  background-color: var(--neutral-50);
  display: inline-block;
  font-size: 0.9rem;
}

.form-footer a:hover {
  color: var(--info-color);
  background-color: var(--neutral-100);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.loading-spinner {
  display: inline-block;
  width: 1.5rem;
  height: 1.5rem;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: rgb(46, 139, 232);
  animation: spin 1s ease-in-out infinite;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    max-width: 450px;
  }
  
  .register-brand {
    padding: 2rem 1.5rem;
  }
  
  .brand-logo {
    font-size: 3.5rem;
    height: 80px;
    width: 80px;
    margin-bottom: 1rem;
  }
  
  .register-brand h1 {
    font-size: 1.75rem;
  }
  
  .register-brand p {
    font-size: 0.9rem;
  }
  
  .register-card {
    padding: 2rem 1.5rem;
  }
  
  .register-card h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
  }
}
</style>